package com.avail.androidutils.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;

/**
 * @project:AndroidUtilsLib
 * @author:zhuguowei
 * @time:2019/12/23 0023 15:27:51
 */
public class IntentUtils {
    /**
     * 跳转到指定应用的首页
     */
    private void showActivity(@NonNull Context context, @NonNull String packageName) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        context.startActivity(intent);
    }

    /**
     * 跳转到指定应用的指定页面
     */
    private void showActivity(@NonNull Context context, @NonNull String packageName, @NonNull String activityDir) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName, activityDir));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    // zgw 2020/3/2 0002 10:16:19 
    private void gotoGetBtMac(Context context) {
        if (!isAutoSleepExist(context)) {
            return;
        }
        Intent intent = new Intent();
        //其中两个参数的含义:第一个是要跳转到的app的包名，第二个参数是该包中的要跳转到app的页面的class
        ComponentName cName = new ComponentName("com.giec.autosleep", "com.giec.autosleep.BluetoothActivity");
        intent.setComponent(cName);
        ((Activity)(context)).startActivityForResult(intent, 1);
    }

    private boolean isAutoSleepExist(Context context) {
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo("com.giec.autosleep", PackageManager.GET_UNINSTALLED_PACKAGES);
//            LogUtil.d(TAG, "com.giec.autosleep is exist 111");
            return true;
        } catch (PackageManager.NameNotFoundException e) {
//            LogUtil.d(TAG, "com.giec.autosleep is not exist 222");
//            toast("请先安装GiecRRS，否则无法获取蓝牙MAC地址");
            return false;
        }
    }
}
